package history.hot100.simple;

import history.hot100.TreeNode;

// 617. 合并二叉树:https://leetcode-cn.com/problems/merge-two-binary-trees/
// https://leetcode-cn.com/problems/merge-two-binary-trees/solution/dong-hua-yan-shi-di-gui-die-dai-617he-bing-er-cha-/
public class LeetCode_617 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null|| root2 == null) return root1 == null? root2:root1;
        return dfs(root1, root2);
    }

    TreeNode dfs(TreeNode t1, TreeNode t2) {
        if (t1 == null|| t2 == null) return t1 == null? t2:t1;
        t1.val += t2.val;;
        t1.left = dfs(t1.left, t2.left);
        t1.right = dfs(t1.right, t2.right);
        return t1;
    }
}
